<?php

Kurogo::includePackage('db');

class PostsWebModule extends WebModule
{
    protected $id = 'posts';
    protected function initializeForPage() {
        $this->addExternalJavascript('http://code.jquery.com/jquery-1.9.1.min.js');
        $this->addExternalJavascript('http://code.jquery.com/jquery-migrate-1.1.1.min.js');
        $this->controller = DataRetriever::factory('FacebookDataRetriever', array());
        switch($this->page) 
        {
            case 'index':                
                $db = new db();
                $postlist = array();
                $sql = "SELECT fbID, message, postID FROM post";
                $result = $db->query($sql, array('fbID','message', 'postID'));
                while ($row = $result->fetch()) {
                    $userID = $row['fbID'];
                    $postID = $row['postID'];
                    $message = $row['message'];
                    $user = $this->controller->getUser($userID);
                    $photo = 'https://graph.facebook.com/' . $userID . '/picture';
                    //LIKE SECTION
                    $session = $this->getSession();
                        if($this->isLoggedIn()){
                            $u = $session->getUser();
                            $uID = $u->getUserID();
                            $login = true;
                            $s = "SELECT * FROM likes WHERE postID = '$postID' AND fbID = '$uID'";
                            $q = $db->query($s,array());
                            $r = $q->fetch();
                            if($r){
                                $likeimg = '/modules/posts/images/like.png';
                            }
                            else {
                                $likeimg = '/modules/posts/images/nolike.png';
                            }

                            //COUNT LIKES
                            $s = "SELECT COUNT(*) as likes FROM likes WHERE postID = '$postID'";
                            $q = $db->query($s,array());
                            $r = $q->fetch();
                            $likes = $r['likes'];

                            //FLAG SECTION
                            $s = "SELECT * FROM flags WHERE postID = '$postID' AND fbID = '$uID'";
                            $q = $db->query($s,array());
                            $r = $q->fetch();
                            if($r){
                                $flagimg = '/modules/posts/images/flag.png';
                            }
                            else {
                                $flagimg = '/modules/posts/images/noflag.png';
                            }
                        }
                        else{
                                $login = false;
                                $likeimg = false;
                                $likes = false;
                                $flagimg = false;
                        }           
                        //COUNT FLAGS
                        $s = "SELECT COUNT(*) as flags FROM flags WHERE postID = '$postID'";
                        $q = $db->query($s,array());
                        $r = $q->fetch();
                        $flags = $r['flags'];

                        if($flags < 5) {
                            $showpost = true;
                        }
                        else {
                            $showpost = false;
                        }
                            
                    $post = array(
                        'title'=>$message,
                        'subtitle'=>$user['name'],
                        'img'=>$photo,
                        'postID'=>$postID,
                        'likeimg'=>$likeimg,
                        'likes'=>$likes,
                        'flagimg'=>$flagimg,
                        'reveal'=>$showpost,
                        'login'=>$login
                    );
                   $postlist[] = $post;
                }
                $this->assign('postlist', $postlist);
                break;
        }
    }
}